package com.vn.ltedatausagetracking.common;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import org.apache.http.HttpStatus;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.google.gson.Gson;
import com.vn.ltedatausagetracking.dto.BaseStatusDto;
import com.vn.ltedatausagetracking.dto.Userdto;

public class Utils {
	public static String convertUserInfoToJson(Userdto userInfo) {
		return new Gson().toJson(userInfo);
	}

	public static BaseStatusDto convertJsonToBaseStatusDto(String json) {
		return new Gson().fromJson(json, BaseStatusDto.class);
	}

	public static boolean putBoolean(Context context, String key, boolean value) {
		SharedPreferences sharedPreferences = PreferenceManager
				.getDefaultSharedPreferences(context);
		SharedPreferences.Editor edit = sharedPreferences.edit();
		edit.putBoolean(key, value);
		return edit.commit();
	}

	public static boolean getBoolean(Context context, String key,
			boolean intDefault) {
		SharedPreferences sharedPreferences = PreferenceManager
				.getDefaultSharedPreferences(context);
		return sharedPreferences.getBoolean(key, intDefault);
	}

	public static boolean putString(Context context, String key, String value) {
		SharedPreferences sharedPreferences = PreferenceManager
				.getDefaultSharedPreferences(context);
		SharedPreferences.Editor edit = sharedPreferences.edit();
		edit.putString(key, value);
		return edit.commit();
	}

	public static String getString(Context context, String key, String defValue) {
		SharedPreferences sharedPreferences = PreferenceManager
				.getDefaultSharedPreferences(context);
		return sharedPreferences.getString(key, defValue);
	}

	public static boolean putInt(Context context, String key, int value) {
		SharedPreferences sharedPreferences = PreferenceManager
				.getDefaultSharedPreferences(context);
		SharedPreferences.Editor edit = sharedPreferences.edit();
		edit.putInt(key, value);
		return edit.commit();
	}

	public static int getInt(Context context, String key, int defValue) {
		SharedPreferences sharedPreferences = PreferenceManager
				.getDefaultSharedPreferences(context);
		return sharedPreferences.getInt(key, defValue);
	}

	public static String sendToServer(String sentURL, String data)
			throws IOException {
		StringBuilder builder = new StringBuilder();

		// connection object
		HttpURLConnection conn = null;

		// stream read from response
		BufferedReader bufferedReader = null;

		try {
			data = URLEncoder.encode(data, "UTF-8");
			data = URLEncoder.encode(data, "UTF-8");
			data = String.format("data=%s", data);
			URL url = new URL(sentURL);
			conn = (HttpURLConnection) url.openConnection();
			conn.setDoOutput(true);
			conn.setDoInput(true);
			conn.setUseCaches(false);
			conn.setRequestMethod("POST");
			conn.setRequestProperty("Content-Length", data.length() + "");
			conn.setRequestProperty("Content-Type",
					"application/x-www-form-urlencoded");
			conn.setConnectTimeout(30000);
			// Time to wait to read data from server
			conn.setReadTimeout(30000 * 4);
			// Send the body
			DataOutputStream dataOS = new DataOutputStream(
					conn.getOutputStream());
			// write head body
			dataOS.writeBytes(data);
			dataOS.flush();
			dataOS.close();

			// Ensure we got the HTTP 200 response code
			int responseCode = conn.getResponseCode();
			if (responseCode != HttpStatus.SC_OK) {
				return null;
			}

			// get output
			bufferedReader = new BufferedReader(new InputStreamReader(
					(conn.getInputStream())));
			String line;
			// reading each line
			while ((line = bufferedReader.readLine()) != null) {
				builder.append(line);
			}

			// close stream
			bufferedReader.close();

			// disconnect
			conn.disconnect();

			return builder.toString();

		} catch (IOException e) {
			throw e;
		} finally {
			// close stream reader
			if (bufferedReader != null) {
				bufferedReader.close();
			}
			// disconnect
			if (conn != null) {
				conn.disconnect();
			}
		}
	}

}
